package com.simple.study.service.impl;

import com.simple.study.base.dao.MyMapper;
import com.simple.study.base.service.impl.BaseServiceImpl;
import com.simple.study.mapper.PermissionMapper;
import com.simple.study.pojo.Permission;
import com.simple.study.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MenusServiceImpl
 * @Author SimpleTree
 **/
@Service
public class MenusServiceImpl extends BaseServiceImpl implements MenusService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public MyMapper getMapper() {
        return permissionMapper;
    }

    @Override
    public List<Permission> getMenus() {

        Permission p = new Permission();
        p.setLevel("0");
        List<Permission> permissions = permissionMapper.select(p);

        p.setLevel("1");
        List<Permission> res = new ArrayList<>();
        for (Permission permission : permissions) {
            p.setPid(permission.getId());
            List<Permission> list = permissionMapper.select(p);
            permission.setChildren(list);
            res.add(permission);
        }
        return res;
    }

    /**
     * type = list
     *
     * @return
     */
    @Override
    public List<Permission> getMenusList() {
        return permissionMapper.selectAll();
    }

    /**
     * type = tree
     *
     * @return
     */
    @Override
    public List<Permission> getMenusTree() {

        //获取一级权限
        List<Permission> firstAll = permissionMapper.getPermissionByLevel("0");
        //遍历一级权限,找到二级权限
        for (Permission permission : firstAll) {
            //找到的二级权限
            List<Permission> permissionByUnio = permissionMapper.getPermissionByUnio(permission.getId());

            //遍历二级权限
            for (Permission sec : permissionByUnio) {
                //找到的三级权限
                List<Permission> thirdList = permissionMapper.getPermissionByUnio(sec.getId());
                //将三级权限加入到二级权限内
                sec.setChildren(thirdList);
            }
            //将二级权限加入到一级权限内
            permission.setChildren(permissionByUnio);
        }
        return firstAll;
    }
}
